package com.example.markingsystem.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;

@RestController
public class WechatController {
/*
    @GetMapping("/wechat/verification")
    public String toindex2(String signature,String timestamp,String nonce,String echostr) throws NoSuchAlgorithmException {
        System.out.println(signature);
        System.out.println(echostr);
        System.out.println(timestamp);
        System.out.println(nonce);
//        List<String> list = Arrays.asList("12345abc",timestamp,nonce);
//
//        Collections.sort(list);
//        StringBuilder stringBuilder = new StringBuilder();
//        for (String li : list){
//            stringBuilder.append(li);
//        }
//        MessageDigest sha1 = MessageDigest.getInstance("sha1");
//
//        byte[] digest = sha1.digest(stringBuilder.toString().getBytes());
//        StringBuilder sum = new StringBuilder();
//        for (byte b : digest){
//            sum.append(Integer.toHexString((b >> 4 & 15)));
//
//            sum.append(Integer.toHexString((b & 15)));
//        }
//
//        if(!StringUtils.isEmpty(signature) && signature.equals(sum.toString())){
//            System.out.println("1233333333333");
//            System.out.println(echostr);
//            return echostr;
//        }
        return echostr;
    }*/

    /*
    @GetMapping("/wechat/verification")
    public String handleCallback(@RequestParam("code") String code) {

        System.out.println("我的code打了复活赛"+code);
        // 在这里你可以使用code换取access_token和openid
        // ...
        // 根据openid执行数据库查询并根据结果跳转至界面1或界面2
        // ...
        return "redirect:/path/to/interface1"; // 或者 "redirect:/path/to/interface2";
    }*/


    @GetMapping("/wechat/authorize")
    public String authorize(@RequestParam("code") String code) {
        // 在这里处理接收到的code参数，可以根据需要进行处理
        System.out.println("我的code拿到了："+code);
        return "code: " + code;
    }

    @GetMapping("/wechat/login")
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String appid = "wxe55cc3e4ab75907b";
        String redirectUri = URLEncoder.encode("http://106.14.141.173:8800/wechat/verification", "UTF-8");
        String scope = "snsapi_base";
        String state = "STATE";

        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                "appid=" + appid +
                "&redirect_uri=" + redirectUri +
                "&response_type=code" +
                "&scope=" + scope +
                "&state=" + state +
                "#wechat_redirect";


        response.sendRedirect(url);
    }

//这一段先不用

}
